/**
 * anji-allways.com Inc.
 * Copyright (c) 2006-2016 All Rights Reserved.
 */
package cn.zhiwen.util;

import java.security.MessageDigest;

/**
 * <pre>
 * MD5工具类
 * </pre>
 * @author guocheng
 * @version $Id: MD5Util.java, v 0.1 2017年4月29日 下午2:04:16 guocheng Exp $
 */
public final class MD5Util {

    private static MD5Util      md5Util  = null;

    private static final String MD5_SALT = "QAZWSXEDCRFV09876543210";

    public static synchronized MD5Util getInstance() {

        if (md5Util == null) {
            md5Util = new MD5Util();
        }
        return md5Util;
    }

    private MD5Util() {

    }

    public String encrypt(String dataStr) {
        try {
            MessageDigest m = MessageDigest.getInstance("MD5");
            m.update(dataStr.getBytes("UTF8"));
            byte[] s = m.digest();
            StringBuilder result = new StringBuilder();
            for (int i = 0; i < s.length; i++) {
                result.append(Integer.toHexString((0x000000FF & s[i]) | 0xFFFFFF00).substring(6));
            }
            return result.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return "";
    }

    /**
     * <pre>
     * md5(md5(str+salt))两次MD5,一次加盐
     * </pre>
     * @param dataStr
     * @return
     */
    public String encryptBySalt(String dataStr) {
        try {
            dataStr = dataStr + MD5_SALT;
            MessageDigest m = MessageDigest.getInstance("MD5");
            m.update(dataStr.getBytes("UTF8"));
            byte[] s = m.digest();
            StringBuilder result = new StringBuilder();
            for (int i = 0; i < s.length; i++) {
                result.append(Integer.toHexString((0x000000FF & s[i]) | 0xFFFFFF00).substring(6));
            }
            String realResult = encrypt(result.toString()); // 第二次MD5
            return realResult;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

}
